package cn.xlmdz.subsoilerinfoquery.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.kevin.delegationadapter.DelegationAdapter;

import java.util.HashMap;
import java.util.Map;

import cn.xlmdz.subsoilerinfoquery.R;
import cn.xlmdz.subsoilerinfoquery.adapter.CarListAdapter;
import cn.xlmdz.subsoilerinfoquery.adapter.QuerySingleListAdapter;
import cn.xlmdz.subsoilerinfoquery.bean.CarDataBean;
import cn.xlmdz.subsoilerinfoquery.bean.OrganizeBean;
import cn.xlmdz.subsoilerinfoquery.databinding.ActivityCarListBinding;
import cn.xlmdz.subsoilerinfoquery.http.ApiEngine;
import cn.xlmdz.subsoilerinfoquery.http.ApiServices;
import cn.xlmdz.subsoilerinfoquery.utils.SharedPrefsUtil;
import cn.xlmdz.subsoilerinfoquery.utils.ToastUtil;
import cn.xlmdz.subsoilerinfoquery.view.LoadingDialog;
import cn.xlmdz.subsoilerinfoquery.view.VerticalSpacingItemDecoration;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class CarListActivity extends AppCompatActivity {

    private ActivityCarListBinding viewBinding;
    private LoadingDialog loadingDialog;

    /*** 委托适配器 */
    private DelegationAdapter mDelegationAdapter;
    private CarListAdapter listAdapterDelegate;

    private String orgId,orgName,parentId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding = ActivityCarListBinding.inflate(getLayoutInflater());
        setContentView(viewBinding.getRoot());

        Intent intent = getIntent();
        orgId = intent.getStringExtra("orgId");
        orgName = intent.getStringExtra("orgName");
        parentId = intent.getStringExtra("parentId");

        initViews();
        initRecyclerView();
        getCarListData();
    }

    private void initViews() {
        viewBinding.tvTitle.setText(orgName);
        viewBinding.toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initRecyclerView() {
        //layoutManager
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        viewBinding.recycleCarList.setLayoutManager(layoutManager);

        //设置item间隔
        VerticalSpacingItemDecoration itemDecoration = new VerticalSpacingItemDecoration(20);
        viewBinding.recycleCarList.addItemDecoration(itemDecoration);

        //设置回收复用池大小--如果一屏内相同类型的 View 个数比较多，需要设置一个合适的大小，防止来回滚动时重新创建 View
        RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
        viewBinding.recycleCarList.setRecycledViewPool(viewPool);
        viewPool.setMaxRecycledViews(0, 15);

        // listView内容适配器
        listAdapterDelegate = new CarListAdapter(this, 0);
        mDelegationAdapter = new DelegationAdapter();
        mDelegationAdapter.addDelegate(listAdapterDelegate);
        viewBinding.recycleCarList.setAdapter(mDelegationAdapter);
    }

    private void getCarListData(){
        showLoading();
        String token = SharedPrefsUtil.getStringPreference(this, "loginToken");
        if (!TextUtils.isEmpty(token)) {
            ApiServices services = ApiEngine.getInstance().getApiService();
            Map<String, String> params = new HashMap<>();
            params.put("org_id", orgId);
            params.put("type", "");
            Observable<CarDataBean> observable = services.getCarListData(token,params);
            observable.subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<CarDataBean>() {
                        @Override
                        public void onSubscribe(@NonNull Disposable d) {

                        }

                        @Override
                        public void onNext(@NonNull CarDataBean carDataBean) {
                            hideLoading();
                            mDelegationAdapter.setDataItems(carDataBean.getData());// 设置list数据
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                            hideLoading();
                        }

                        @Override
                        public void onComplete() {
                            hideLoading();
                        }
                    });
        }
    }

    public void showLoading() {
        if (loadingDialog == null)
            loadingDialog = new LoadingDialog(this, "加载中…");
        if (!loadingDialog.isShowingNew())
            loadingDialog.show();
    }

    public void hideLoading() {
        if (loadingDialog != null && loadingDialog.isShowingNew())
            loadingDialog.close();
    }
}